home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / LSOPS.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  179 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsops.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDLIB
  11. #include <stdlib.h>
  12. #endif
  13.  
  14. /* non-ansi headers */
  15. #include <bool.h>
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19.  
  20. /* local headers */
  21. #include "lseq_.h"
  22.  
  23. /*man---------------------------------------------------------------------------
  24. NAME
  25.      ls_alloc - allocate memory for a lseq
  26.  
  27. SYNOPSIS
  28.      #include "lseq_.h"
  29.  
  30.      int ls_alloc(lsp);
  31.      lseq_t *lsp;
  32.  
  33. DESCRIPTION
  34.      The ls_alloc function allocates the memory needed by lseq lsp.
  35.      The memory is initialized to 0.
  36.  
  37.      ls_alloc will fail if one or more of the following is true:
  38.  
  39.      [EINVAL]       lsp is not a valid lseq pointer.
  40.      [ENOMEM]       Not enough memory is available for
  41.                     allocation by the calling process.
  42.      [LSENOPEN]     lsp is not open.
  43.  
  44. SEE ALSO
  45.      ls_free.
  46.  
  47. DIAGNOSTICS
  48.      Upon successful completion, a value of 0 is returned.  Otherwise,
  49.      a value of -1 is returned, and errno set to indicate the error.
  50.  
  51. ------------------------------------------------------------------------------*/
  52. #ifdef AC_PROTO
  53. int ls_alloc(lseq_t *lsp)
  54. #else
  55. int ls_alloc(lsp)
  56. lseq_t *lsp;
  57. #endif
  58. {
  59. #ifdef DEBUG
  60.     /* validate arguments */
  61.     if (!ls_valid(lsp)) {
  62.         LSEPRINT;
  63.         errno = EINVAL;
  64.         return -1;
  65.     }
  66.  
  67.     /* check if not open */
  68.     if (!(lsp->flags & LSOPEN)) {
  69.         LSEPRINT;
  70.         errno = LSENOPEN;
  71.         return -1;
  72.     }
  73.  
  74.     /* check for memory leak */
  75.     if (lsp->clsrp != NULL) {
  76.         LSEPRINT;
  77.         errno = LSEPANIC;
  78.         return -1;
  79.     }
  80. #endif
  81.     /* current record */
  82.     lsp->clsrp = ls_rcalloc(lsp);
  83.     if (lsp->clsrp == NULL) {
  84.         LSEPRINT;
  85.         return -1;
  86.     }
  87.  
  88.     return 0;
  89. }
  90.  
  91. /*man---------------------------------------------------------------------------
  92. NAME
  93.      ls_blksize - lseq block size
  94.  
  95. SYNOPSIS
  96.      #include <lseq.h>
  97.  
  98.      size_t ls_blksize(lsp)
  99.      lseq_t *lsp;
  100.  
  101. DESCRIPTION
  102.      ls_blksize returns the size of the blocks in lseq lsp.  If lsp is
  103.      not a valid open lseq, the results are undefined.  ls_blksize is
  104.      a macro.
  105.  
  106. ------------------------------------------------------------------------------*/
  107. /* ls_blksize defined in lseq_.h. */
  108.  
  109. /*man---------------------------------------------------------------------------
  110. NAME
  111.      ls_free - free memory allocated for an lseq
  112.  
  113. SYNOPSIS
  114.      #include "lseq_.h"
  115.  
  116.      void ls_free(lsp)
  117.      lseq_t *lsp;
  118.  
  119. DESCRIPTION
  120.      The ls_free function frees all memory allocated for lseq lsp.
  121.      If lsp is not a valid lseq, no action is taken.
  122.  
  123. SEE ALSO
  124.      ls_alloc.
  125.  
  126. ------------------------------------------------------------------------------*/
  127. #ifdef AC_PROTO
  128. void ls_free(lseq_t *lsp)
  129. #else
  130. void ls_free(lsp)
  131. lseq_t *lsp;
  132. #endif
  133. {
  134. #ifdef DEBUG
  135.     /* validate arguments */
  136.     if (!ls_valid(lsp)) {
  137.         LSEPRINT;
  138.         return;
  139.     }
  140. #endif
  141.     /* free memory */
  142.     ls_rcfree(lsp->clsrp);
  143.     lsp->clsrp = NULL;
  144.  
  145.     return;
  146. }
  147.  
  148. /*man---------------------------------------------------------------------------
  149. NAME
  150.      ls_valid - validate lseq
  151.  
  152. SYNOPSIS
  153.      #include "lseq_.h"
  154.  
  155.      bool ls_valid(lsp)
  156.      lseq_t *lsp;
  157.  
  158. DESCRIPTION
  159.      The ls_valid function determines if lsp is a valid lseq pointer.
  160.      If valid, then TRUE is returned.  If not, then FALSE is returned.
  161.  
  162. ------------------------------------------------------------------------------*/
  163. #ifdef AC_PROTO
  164. bool ls_valid(lseq_t *lsp)
  165. #else
  166. bool ls_valid(lsp)
  167. lseq_t *lsp;
  168. #endif
  169. {
  170.     if (lsp < lsb || lsp > (lsb + LSOPEN_MAX - 1)) {
  171.         return FALSE;
  172.     }
  173.     if ((((char *)lsp - (char *)lsb)) % sizeof(*lsb) != 0) {
  174.         return FALSE;
  175.     }
  176.  
  177.     return TRUE;
  178. }
  179.